import json

human_json_path = "/home/jxy/datasets/gen-vlkt/hico_20160224_det/annotations/trainval_hico.json"
# frame_json_path = "/home/jxy/program/hoi2/600HOI-600HOI_no_object-add_seg_only_instance-max_adapt-dynamic_threshold_add_weight_23-black_add_correlation_hoi/output/frame_pred_tags.json"
# frame_json_path = "/home/jxy/program/hoi2/600HOI-600HOI_no_object-add_seg_only_instance-max_adapt-dynamic_threshold_add_weight_23-black_add_correlation_hoi/600HOI-600HOI_no_object-add_seg_only_instance-max_adapt-dynamic_threshold-black_add_correlation_hoi.json"
frame_json_path = "/home/jxy/program/hoi2/600HOI-600HOI_no_object-add_seg_only_instance-max_adapt-dynamic_threshold_add_weight_23-black_add_correlation_hoi/results/renew_one_p_o.json"
with open(human_json_path, 'r', encoding='utf8') as f1, open(frame_json_path, 'r', encoding='utf8') as f2:
    human_json_data = json.load(f1)
    frame_json_data = json.load(f2)

def calculate_accuracy(human_hoi, frame_hoi):
    # 从human_hoi的第一个元素中提取hoi_category_id并保存，使用set去除重复项
    human_hoi_category_ids = set(anno['hoi_category_id'] for anno in human_hoi['hoi_annotation'])

    # 从frame_hoi的第一个元素中提取hoi_category_id并保存，使用set去除重复项
    frame_hoi_category_ids = set(anno['hoi_category_id'] for anno in frame_hoi['hoi_annotation'])

    # 打印提取的hoi_category_id列表
    print("人工标注的HOI_CATEGORY_ID：", human_hoi_category_ids)
    print("框架标注的HOI_CATEGORY_ID：", frame_hoi_category_ids)

    # 计算框架标注正确的个数
    correct_predictions = sum(1 for hoi_id in frame_hoi_category_ids if hoi_id in human_hoi_category_ids)

    # 计算人工标签的总个数
    total_human_annotations = len(human_hoi_category_ids)

    # 计算准确率
    accuracy = correct_predictions / total_human_annotations

    # 返回准确率
    return accuracy
 
accuracies = []
# 检查两个列表是否具有相同的长度
if len(human_json_data) != len(frame_json_data):
    print("JSON 数据长度不一致")
else:
    # 逐个比较元素
    for human_item, frame_item in zip(human_json_data, frame_json_data):
        # 调用计算准确率函数并传递当前的人工标注和框架标注元素
        print(human_item['file_name'])

        result_accuracy = calculate_accuracy(human_item, frame_item)
        accuracies.append(result_accuracy)  # 添加准确率到列表中

        print("准确率：", result_accuracy)
        
# 计算平均准确率
average_accuracy = sum(accuracies) / len(accuracies)
print("平均准确率：", average_accuracy)
